import javax.swing.JFrame;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author edhem.brakmic
 * 
 */

public class NewTable {

	JFrame frame;
	JList callList;
	private JTextField textField;
	private JTextField textField_1;

	// Table name
	private String tblName = "";

	// Field names and types
	private List<String> tblFields;
	private List<String> tblTypes;

	/**
	 * Create the application.
	 */
	public NewTable(JList list_1) {
		this.callList=list_1;
		initialize();
	}


	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		final DbConnector db = new DbConnector();
		tblFields = new ArrayList<String>();
		tblTypes = new ArrayList<String>();

		frame = new JFrame();
		frame.setBounds(100, 100, 442, 424);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JLabel lblCreateNewTable = new JLabel(
				"Create New Table(Define Payload)");
		lblCreateNewTable.setBounds(120, 12, 214, 15);
		frame.getContentPane().add(lblCreateNewTable);

		JSeparator separator = new JSeparator();
		separator.setBounds(12, 34, 412, 2);
		frame.getContentPane().add(separator);

		textField = new JTextField();
		textField.setBounds(171, 48, 240, 19);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblTableName = new JLabel("Table name:");
		lblTableName.setBounds(37, 50, 116, 15);
		frame.getContentPane().add(lblTableName);

		textField_1 = new JTextField();
		textField_1.setBounds(171, 104, 240, 19);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);

		JLabel lblFieldName = new JLabel("Field name:");
		lblFieldName.setBounds(37, 106, 116, 15);
		frame.getContentPane().add(lblFieldName);

		JLabel lblFieldType = new JLabel("Field type:");
		lblFieldType.setBounds(37, 144, 116, 15);
		frame.getContentPane().add(lblFieldType);

		List<String> comEntries = new ArrayList<String>();
		comEntries.add("int16");
		comEntries.add("int32");
		comEntries.add("int64");
		comEntries.add("float(32bit)");
		comEntries.add("double(64bit)");

		final JComboBox comboBox = new JComboBox(comEntries.toArray());
		comboBox.setBounds(171, 139, 240, 24);
		frame.getContentPane().add(comboBox);

		JButton btnSave = new JButton("Save");
		btnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (textField.getText() != "") {
					try {
						db.CreateTable(textField.getText(),
								tblFields.toArray(), tblTypes.toArray());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						
						callList.removeAll();
						callList.setListData(db.getTableNames().toArray());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					frame.dispose();

				}
			}
		});
		btnSave.setBounds(110, 358, 107, 25);
		frame.getContentPane().add(btnSave);

		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				frame.dispose();

			}
		});
		btnNewButton.setBounds(246, 358, 107, 25);
		frame.getContentPane().add(btnNewButton);

		final DefaultListModel model = new DefaultListModel();

		final JList list = new JList(model);
		list.setBounds(0, 0, 2, 6);
		frame.getContentPane().add(list);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setVisibleRowCount(-1);

		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(110, 211, 243, 135);
		frame.getContentPane().add(scrollPane);

		JButton btnAdd = new JButton("Add");
		btnAdd.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				String tmp = "";
				if (comboBox.getSelectedItem() != null) {
					tmp = textField_1.getText() + " | "
							+ comboBox.getSelectedItem().toString();
				}
				tblFields.add(textField_1.getText());

				if (comboBox.getSelectedItem().toString().equals("int16")) {
					tblTypes.add("smallint");
				}

				if (comboBox.getSelectedItem().toString().equals("int32")) {
					tblTypes.add("integer");
				}

				if (comboBox.getSelectedItem().toString().equals("int64")) {
					tblTypes.add("bigint");
				}
				if (comboBox.getSelectedItem().toString()
						.equals("float(32bit)")) {
					tblTypes.add("real");
				}
				if (comboBox.getSelectedItem().toString().equals(
						"double(64bit)")) {
					tblTypes.add("double precision");
				}

				int pos = list.getModel().getSize();
				model.add(pos, tmp);
				

			}
		});
		btnAdd.setBounds(110, 174, 107, 25);
		frame.getContentPane().add(btnAdd);

		JButton btnDelete = new JButton("Delete");
		btnDelete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int pos = list.getSelectedIndex();
				if (pos >= 0) {

					tblFields.remove(pos);
					tblTypes.remove(pos);

					model.remove(pos);

				}

			}
		});
		btnDelete.setBounds(246, 174, 107, 25);
		frame.getContentPane().add(btnDelete);

	}
}
